<template>
  <nav class="app-topnav">
    <div class="container">
      <ul>
        <template v-if="profile.token">
          <li>
            <a href="javascript:;"><i class="iconfont icon-user"></i>{{ profile.nickname }}</a>
          </li>
          <li><a @click="goToLogout" href="javascript:;">退出登录</a></li>
        </template>
        <template v-else>
          <li><a href="javascript:;" @click="goLogin">请先登录</a></li>
          <li><a href="javascript:;">免费注册</a></li>
        </template>
        <li><a href="javascript:;">我的订单</a></li>
        <li><a href="javascript:;" @click="getUser">会员中心</a></li>
        <li><a href="javascript:;">帮助中心</a></li>
        <li><a href="javascript:;">关于我们</a></li>
        <li class="mobile">
          <a href="javascript:;"><i class="iconfont icon-phone"></i>手机版</a>
        </li>
      </ul>
    </div>
  </nav>
</template>

<script>
import { useStore } from 'vuex'
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import Confirm from 'erabbit-ui/packages/components/Confirm'
import Message from 'erabbit-ui/packages/components/Message'
export default {
  setup() {
    const store = useStore() // 组合api获取vuex的数据方法
    const router = useRouter()
    const profile = computed(() => {
      return store.state.user.profile
    })
    const goLogin = () => {
      router.push('/login')
    }
    const getUser = () => {
      router.push('/user')
    }
    const goToLogout = async () => {
      try {
        await Confirm({ title: '温馨提示!', text: '您确定退出登录吗?' })
        router.push('/login')
        store.commit('user/deleteProfile')
        store.commit('cart/setList', [])
        Message({ type: 'success', text: '已退出登录。' })
      } catch (error) {
        Message({ type: 'success', text: '已取消退出登录。' })
      }
    }
    return { profile, goLogin, getUser, goToLogout }
  }
}
</script>

<style lang="less">
.app-topnav {
  background-color: #333;
  ul {
    display: flex;
    height: 53px;
    justify-content: flex-end;
    align-items: center;
    li {
      border-right: 1px solid #666;
      a {
        padding: 0 15px;
        color: #cdcdcd;
        line-height: 1;
        display: inline-block;
        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
      a:hover {
        color: var(--xtx-color);
      }
    }
    .mobile {
      border-right: 0;
    }
  }
}
</style>
